package com.sevattal.service.leaves.controller;


import com.sevattal.common.base.result.R;
import com.sevattal.common.base.util.JwtInfo;
import com.sevattal.common.base.util.JwtUtils;
import com.sevattal.service.leaves.entity.Emp;
import com.sevattal.service.leaves.entity.vo.EmpLoginVo;
import com.sevattal.service.leaves.entity.vo.EmpVo;
import com.sevattal.service.leaves.service.EmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Api(tags = "员工管理")
@RestController
@RequestMapping("/admin/leaves/emp")
public class EmpController {
    @Autowired
    private EmpService empService;

    @ApiOperation("新建员工")
    @PostMapping("createEmp")
    public R createEmp(@ApiParam(value = "员工对象",required = true) @RequestBody Emp emp){
        boolean result = empService.createEmp(emp);
        if (result) {
            return R.ok().message("创建成功");
        }else{
            return R.error().message("创建失败");
        }
    }

    @ApiOperation("删除员工")
    @DeleteMapping("deleteEmp/{id}")
    public R  deleteEmp(@ApiParam(value = "员工ID",required = true) @PathVariable String id){
        boolean result = empService.deleteEmp(id);
        if (result) {
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("更新员工")
    @PutMapping("updateEmp")
    public R  updateEmp(@ApiParam(value = "员工对象",required = true) @RequestBody Emp emp){
        boolean result = empService.updateEmp(emp);
        if (result) {
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("根据id获取员工信息")
    @GetMapping("get/{id}")
    public R  getEmp(@ApiParam(value = "员工ID",required = true) @PathVariable String id){
        EmpVo result = empService.getByEmpId(id);
        return R.ok().data("emp",result);
    }

    @ApiOperation("根据token获取员工信息")
    @GetMapping("info")
    public R getInfo(String token){
        System.out.println(token);
        JwtInfo jwtInfo = JwtUtils.getUserIdByJwtToken(token);
        if (jwtInfo != null){
            return R.ok().data("name",jwtInfo.getName()).data("roles",jwtInfo.getRoles()).
                    data("id",jwtInfo.getId()).data("avatar",jwtInfo.getAvatar());
        }else {
            return R.error().message("验证失败");
        }
    }

    @ApiOperation("获取员工信息列表")
    @GetMapping("getList")
    public R  getEmpList(){
        List<EmpVo> result = empService.getEmptList();
        return R.ok().data("empList" ,result);
    }

    @ApiOperation("登录")
    @PostMapping("login")
    public R login(@ApiParam(value =" 账号密码",required = true) @RequestBody EmpLoginVo empLoginVo){
        String token = empService.login(empLoginVo);
        return R.ok().data("token",token);
    }

    @ApiOperation("登出")
    @PostMapping("logout")
    public R empLogout(HttpServletRequest request){
        return R.ok().message("退出登录");
    }

    @ApiOperation("按照查询条件查询员工列表")
    @PostMapping("getListBySearch")
    public R getListBySearch(@ApiParam(value = "查询emp条件",required = true) @RequestBody EmpVo empVo){
        List<EmpVo> result = empService.getListBySearch(empVo);
        return R.ok().data("empList",result);

    }


}

